/*
 * Copyright (c) 2013 WDCY Information Technology Co. Ltd
 * www.wdcy.cc
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * WDCY Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with WDCY Information Technology Co. Ltd.
 */
package com.monkey.web.shiro.authz.permission;

import com.monkey.web.platform.service.RoleService;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Wu Tianqiang
 */
public class MonkeyRolePermissionResolver implements RolePermissionResolver {

    @Autowired
    private RoleService roleService;

    @Override
    public Collection<Permission> resolvePermissionsInRole(String roleString) {
        Set<Permission> permissions = new HashSet<>();
        Set<String> permissionsStrings = roleService.resolvePermissionsInRole(roleString);
        for (String permissionString : permissionsStrings) {
            permissions.add(resolvePermission(permissionString));
        }
        return permissions;
    }


    public Permission resolvePermission(String permissionString) {
        return new WildcardPermission(permissionString);
    }
}